<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../rest.css">
    <style>
        .con {
            width: 1220px;
            margin: 0 auto;
        }

        .header {
            height: 200px;
            background: pink;
        }

        .banner {
            height: 500px;
            background: aquamarine
        }

        .main .part {
            height: 400px;
            border: 1px solid #333;
            text-align: center;
            line-height: 400px;
            box-sizing: border-box;
        }

        .footer {
            height: 500px;
            background-color: #333;
        }

        .asideNav {
            font-size: 12px;
            text-align: center;
            line-height: 40px;
            padding: 10px 5px;
            background-color: #f6f6f6;
            position: absolute;
            right: 50px;
            top: 200px;
            transition: top 0.5s;
        }

        .asideNav .active {
            background-color: #00a1d6;
            color: #fff;
        }

        .asideNav.fixed {
            position: fixed;
            top: 50%;
            transform: translateY(-50%);
        }
    </style>
</head>
<body>
    <div class="header con"></div>
    <div class="banner con"></div>
    <div class="main con">
        <div class="part">part-01</div>
        <div class="part">part-02</div>
        <div class="part">part-03</div>
        <div class="part">part-04</div>
        <div class="part">part-05</div>
        <div class="part">part-06</div>
        <div class="part">part-07</div>
        <div class="part">part-08</div>
        <div class="part">part-09</div>
        <div class="part">part-10</div>
    </div>
    <div class="footer"></div>
    <!----------------------------------------------------------- -->
    <div class="asideNav">
        <div class="nav">
            <li>nav_01</li>
            <li>nav_02</li>
            <li>nav_03</li>
            <li>nav_04</li>
            <li>nav_05</li>
            <li>nav_06</li>
            <li>nav_07</li>
            <li>nav_08</li>
            <li>nav_09</li>
            <li>nav_10</li>
            <div class="back">top</div>
        </div>
    </div>
</body>
<script>
    var header = document.querySelector('.header')
    var asideNav = document.querySelector('.asideNav')
    var liList = document.querySelectorAll('.nav li')
    var back = document.querySelector('.back')
    var partList = document.querySelectorAll(".main .part")
    var main = document.getElementsByClassName("main")[0]
    //调用
    JCscroll()
    //在窗口滚动条变化的时候再次调用进行判断更改
    window.onscroll = JCscroll
    for (let i = 0; i < liList.length; i++) {
        //绑定点击事件
        liList[i].onclick = function () {
            //这个i的问题要注意，不能使用全局变量i来进行绑定！！！！！！！！！！
            // liList[i].classList.add('active')
            for (j = 0; j < liList.length; j++) {
                liList[j].className = ''
            }
            //进行点击跳转，先获取需要跳转的元素的相对于window的位置，在让window的滚动条平滑滚动到指定位置
            var scrolltop = partList[i].offsetTop
            window.scrollTo({
                top: scrolltop,
                behavior: "smooth"
            })
        }
    }
    //返回顶部
    back.onclick = function () {
        document.documentElement.scrollTop = 0
    }
    //封装判断窗口的滚动条位置判断
    function JCscroll() {
        var scrolltop = document.documentElement.scrollTop
        if (scrolltop >= header.clientHeight) {
            asideNav.classList.add('fixed')
        } else {
            asideNav.classList.remove('fixed')
        }
        //根据界面内容更改nav的活跃状态 
        //先判断是否在part界面内
        if (scrolltop >= main.offsetTop && scrolltop <= main.offsetTop + main.clientHeight) {
            for (let i = 0; i < partList.length; i++) {
                let part = partList[i]
                var start = part.offsetTop
                var end = part.offsetTop + part.clientHeight
                //给nav添加活跃状态
                if (scrolltop >= start && scrolltop <= end) {
                    for (var j = 0; j < liList.length; j++) {
                        liList[j].className = ""
                    }
                    liList[i].className = "active"
                }
            }
        } else {
            //如果不在main part的内容内的话 就把活跃状态全部清除
            for (var j = 0; j < liList.length; j++) {
                liList[j].className = ""
            }
        }
    }

</script>

</html>